<template>
	<view>
		<scroll-view ref="tabbar1" id="tab-bar" class="tab-bar" :scroll="false" :scroll-x="true" :show-scrollbar="false"
			:scroll-into-view="scrollInto">
			<view style="flex-direction: column;justify-content: space-between;">
				<view style="flex-direction: row;justify-content: space-between;width:750rpx;">
					<view class="uni-tab-item" v-for="(tab,index) in tabs" :key="tab.id" :id="tab.id"
						:ref="'tabitem'+index" :data-id="index" :data-current="index" @click="ontabtap">
						<text class="uni-tab-item-title"
							:class="value==index ? 'uni-tab-item-title-active' : ''">{{tab.name}}</text>
					</view>
				</view>
				<view class="scroll-view-indicator">
					<view ref="underline" class="scroll-view-underline" :class="isTap ? 'scroll-view-animation':''"
						:style="{left: indicatorLineLeft + 'px', width: indicatorLineWidth + 'px'}"></view>
				</view>
			</view>
		</scroll-view>
		<view class="tab-bar-line"></view>
	</view>
</template>


<script>
	// #ifdef APP-PLUS
	const dom = weex.requireModule('dom');
	// #endif
	
	export default {
		props: {
			tabs: { // 支持格式: ['全部', '待付款'] 或 [{name:'全部'}, {name:'待付款'}]
				type: Array,
				default(){
					return []
				}
			},
			value: { // 当前显示的下标 (使用v-model语法糖: 1.props需为value; 2.需回调input事件)
				type: [Number],
				default: 0
			},
		},
		watch: {
			tabs() {
				//console.log(999);
				this.init()
			},
			value() {
				//console.log(888);
				this.init()
			}
		},
		data() {
			return {
				scrollInto:'',//滚动到哪个元素
				isTap: false,//当切换tab时给滑动的动画
				indicatorLineLeft: 0,
				indicatorLineWidth: 0,
			}
		},
		mounted() {
			this.init()
		},
		methods: {
			init(){
				this.$nextTick(()=>{
					this._lastTabIndex = 0;
					this.tabbarWidth = 0;
					this.tabListSize = [];//每一个tabs的属性信息
					this._touchTabIndex = 0;
					//获取元素信息
					this.selectorQuery();
				})
			},
			ontabtap(e) {
				let index = e.target.dataset.current || e.currentTarget.dataset.current;
				//console.log('index',index);
				//let offsetIndex = this._touchTabIndex = Math.abs(index - this._lastTabIndex) > 1;
			
				// #ifdef APP-PLUS || H5 || MP-WEIXIN || MP-QQ
				this.isTap = true;
				var currentSize = this.tabListSize[index];
				this.updateIndicator(currentSize.left, currentSize.width);
				this._touchTabIndex = index;
				// #endif
			
				this.switchTab(index);
			},
			switchTab(index) {
				// if (this.pageList[index].dataList.length === 0) {
				// 	this.loadTabData(index);
				// }
			
				if(this.value!=index){
					this.$emit("input",index);
					this.$emit("change",index);
				}
				
			
			// 	// 缓存 tabId
			// 	if (this.pageList[this.tabIndex].dataList.length > MAX_CACHE_DATA) {
			// 		let isExist = this.cacheTab.indexOf(this.tabIndex);
			// 		if (isExist < 0) {
			// 			this.cacheTab.push(this.tabIndex);
			// 		}
			// 	}
			
			// 	this.tabIndex = index;
			
			// 	// #ifdef APP-NVUE
			// 	this.scrollTabTo(index);
			// 	// #endif
			// 	// #ifndef APP-NVUE
			// 	this.scrollInto = this.tabList[index].id;
			// 	// #endif
			
			// 	// 释放 tabId
			// 	if (this.cacheTab.length > MAX_CACHE_PAGE) {
			// 		let cacheIndex = this.cacheTab[0];
			// 		this.clearTabData(cacheIndex);
			// 		this.cacheTab.splice(0, 1);
			// 	}
			},
			selectorQuery() {
				// #ifdef APP-NVUE
				dom.getComponentRect(this.$refs.tabbar1, res => {
					this.tabbarWidth = res.size.width;
				});
				// for (var i = 0; i < this.tabList.length; i++) {
				// 	this.getElementSize(dom, this.$refs['tabitem' + i][0], i);
				// }
				// 因 nvue 暂不支持 class 查询
				var queryTabSize = uni.createSelectorQuery().in(this);
				for (var i = 0; i < this.tabs.length; i++) {
					queryTabSize.select('#' + this.tabs[i].id).boundingClientRect();
				}
				queryTabSize.exec(rects => {
					rects.forEach((rect) => {
						this.tabListSize[rect.dataset.id] = rect;
					})
					this.updateIndicator(this.tabListSize[this.value].left, this.tabListSize[this.value]
						.width);
					// this.switchTab(this.value);
				});
				// #endif
			
				// #ifdef MP-WEIXIN || H5 || MP-QQ
				uni.createSelectorQuery().in(this).selectAll('.uni-tab-item').boundingClientRect((rects) => {
					rects.forEach((rect) => {
						this.tabListSize[rect.dataset.id] = rect;
					})
					//console.log(rects);
					//设置tab底部选中的下划线位置及长度
					this.updateIndicator(this.tabListSize[this.value].left, this.tabListSize[this.value]
						.width);
				}).exec();
				// #endif
			},
			//非nvue 设置tab底部选中的下划线位置及长度
			updateIndicator(left, width) {
				this.indicatorLineLeft = left;
				this.indicatorLineWidth = width;
			},
			//因 nvue 暂不支持 class 查询所有舍弃
			getElementSize(dom, ref, id) {
				dom.getComponentRect(ref, res => {
					this.tabListSize[id] = res.size;
				});
			},
		}
	}
</script>

<style>
	/* #ifndef APP-PLUS */
	page {
		width: 100%;
		min-height: 100%;
		display: flex;
	}
	
	/* #endif */
	
	.tabs {
		flex: 1;
		flex-direction: column;
		overflow: hidden;
		background-color: #ffffff;
		/* #ifdef MP-ALIPAY || MP-BAIDU */
		height: 100vh;
		/* #endif */
	}
	
	.tab-bar {
		/* #ifdef APP-PLUS */
		width: 750rpx;
		/* #endif */
		height: 42px;
		flex-direction: row;
		/* #ifndef APP-PLUS */
		white-space: nowrap;
		/* #endif */
	}
	
	/* #ifndef APP-NVUE */
	.tab-bar ::-webkit-scrollbar {
		display: none;
		width: 0 !important;
		height: 0 !important;
		-webkit-appearance: none;
		background: transparent;
	}
	
	/* #endif */
	
	.scroll-view-indicator {
		position: relative;
		height: 2px;
		background-color: transparent;
	}
	
	.scroll-view-underline {
		position: absolute;
		top: 0;
		bottom: 0;
		width: 0;
		background-color: #007AFF;
	}
	
	.scroll-view-animation {
		transition-duration: 0.2s;
		transition-property: left;
	}
	
	.tab-bar-line {
		height: 1px;
		background-color: #cccccc;
	}
	
	.tab-box {
		flex: 1;
	}
	
	.uni-tab-item {
		/* #ifndef APP-PLUS */
		display: inline-block;
		/* #endif */
		flex-wrap: nowrap;
		padding-left: 20px;
		padding-right: 20px;
	}
	
	.uni-tab-item-title {
		color: #555;
		font-size: 15px;
		height: 40px;
		line-height: 40px;
		flex-wrap: nowrap;
		/* #ifndef APP-PLUS */
		white-space: nowrap;
		/* #endif */
	}
	
	.uni-tab-item-title-active {
		color: #007AFF;
	}
	
	.swiper-item {
		flex: 1;
		flex-direction: column;
	}
	
	.page-item {
		flex: 1;
		flex-direction: row;
		position: absolute;
		left: 0;
		top: 0;
		right: 0;
		bottom: 0;
	}
	
</style>
